//
//  AddressPickerViewController.m
//  nonghubao
//
//  Created by Fenrir on 13-8-15.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "AddressPickerViewController.h"
#import "Helper.h"
#import "Config.h"
#import "ConsigneeFormViewController.h"

@interface AddressPickerViewController ()

@end

@implementation AddressPickerViewController
@synthesize delegate = _delegate;
@synthesize selectedAddrId = _selectedAddrId;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithSelectedSelectedAddrId:(NSInteger)selectedAddrId
{
    self = [super init];
    
    if (self) {
        _selectedAddrId = selectedAddrId;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.title = _L(@"vc_title_093");
    
    UIButton* manageButton = [Helper buttonWithStyle:ButtonStyleNavigationManage];
    [manageButton addTarget:self action:@selector(manageButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:manageButton];
    
    self.addButton.alpha = 0.0f;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view delegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    
    UIView *accView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 65.0f, 20.0f)];
    accView.alpha = 0.0f;
    
    if (_managing) {
        
        cell.accessoryView = nil;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else {
        
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        [self checkmarkCell:cell andIndexPath:indexPath];
    }

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_managing) {
        
        [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    }
    else {
        
        if ([_delegate respondsToSelector:@selector(addressPicked:)]) {
            [_delegate addressPicked:super.dataSource[indexPath.section]];
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - 私有方法

//标记被选中的cell
- (void)checkmarkCell:(UITableViewCell *)cell andIndexPath:(NSIndexPath *)indexPath
{
    
    //当用户未选择地址时，并且此地址是用户设置的默认地址，那么就标记它。
    //当用户选择地址时，标记它。
    
    if (_selectedAddrId != 0 && [self.dataSource[indexPath.section][@"id"] integerValue] == _selectedAddrId) {
        
        UIImageView* checkImageView = [[UIImageView alloc] initWithFrame:CGRectMake(290, 13, 45/2.5f, 47/2.5f)];
        
        checkImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"transaction_icon_004@2x" ofType:@"png"]];
        
        cell.accessoryView = checkImageView;
    }
    else {
        cell.accessoryView = nil;
    }
}

#pragma mark - 事件

- (void)manageButtonEvent
{
    _managing = !_managing;
    
    [self setViewNeedDisplay:_managing];

    if (_managing) {
        self.title = _L(@"vc_title_011");
        UIButton* exitManageButton = [Helper buttonWithStyle:ButtonStyleNavigationExitManage];
        [exitManageButton addTarget:self action:@selector(manageButtonEvent) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:exitManageButton];
    }
    else {
        self.title = _L(@"vc_title_093");
        UIButton* manageButton = [Helper buttonWithStyle:ButtonStyleNavigationManage];
        [manageButton addTarget:self action:@selector(manageButtonEvent) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:manageButton];
    }
    [self.tableView reloadData];
}

/*!
 @abstract 通过数据源是否为空来判断页面的显示
 */
- (void)viewNeedsDisplay
{
    
    if (self.dataSource.count == 0) {
        
        _blankPage = [[UIView alloc] initWithFrame:CGRectMake(0.0f, -44.0f, 320.0f, _SYS_FRAME_HEIGHT)];
        _blankPage.backgroundColor = [UIColor whiteColor];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 139/2.0f, 137.0f/2.0f)];
        imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"transaction_img_013@2x" ofType:@"png"]];
        imageView.center = _blankPage.center;
        [_blankPage addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, imageView.frame.origin.y+imageView.frame.size.height+10.0f, 300.0f, 50.0f)];
        label.textAlignment = UITextAlignmentCenter;
        label.numberOfLines = 0;
        label.textColor = [UIColor grayColor];
        label.text = _L(@"alert_104");
        [_blankPage addSubview:label];
        
        [self.view addSubview:_blankPage];
        
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.view.backgroundColor = [UIColor whiteColor];
    }else{
        
        [_blankPage removeFromSuperview];
        self.tableView.backgroundColor = _SYS_COLOR_BACKGROUND;
        self.view.backgroundColor = _SYS_COLOR_BACKGROUND;
    }
}
@end
